function [hue y s] = rgb2ihls(I)
%RGB2IHLS converts RGB image I to IHLS color space returning the individual
%   channels hue HUE(in degrees), luminance Y and saturation S

R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);

s = max(I,[],3) - min(I,[],3);      % saturation
y = 0.2125*R + 0.7154*G + 0.0721*B; % luminance 

% chrominance coordinates
crx = R - (G+B)/2;  
cry = sqrt(3)/2*(B-G);
cr = sqrt(crx.^2 + cry.^2);     % chroma

% hue
hue = zeros(size(R));
hue(cr==0) = NaN;
hue(cry<=0) = acosd(crx(cry<=0)./cr(cry<=0));
hue(~(cr==0|cry<=0)) = 360 - acosd(crx(~(cr==0|cry<=0))./cr(~(cr==0|cry<=0)));
end

